package com.pucong.gitee.database.backup.database.util;

import com.pucong.gitee.database.backup.common.entity.BackupDatabase;
import com.pucong.gitee.database.backup.common.selfenum.DatabaseDriverClassEnum;
import com.pucong.gitee.database.backup.configuration.exception.BusinessException;
import com.pucong.gitee.database.backup.database.mapper.BackupExeSqlLogMapper;
import org.apache.commons.lang3.StringUtils;

public class BackupServiceOracleImpl extends AbstractBackupService {

    private static final String URL_PREFIX = "jdbc:oracle:thin:@";

    private static final String URL_LAST = "useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true";

    public BackupServiceOracleImpl(BackupDatabase backupDatabase, BackupExeSqlLogMapper backupExeSqlLogMapper) {
        super(backupDatabase,backupExeSqlLogMapper);
    }

    @Override
    public void initUrl() {
        this.url = URL_PREFIX + backupDatabase.getHost() + ":" + backupDatabase.getPort() + ":" + backupDatabase.getDatabaseName();
        this.driverClassName = DatabaseDriverClassEnum.oracle.getClassName();
    }

    @Override
    public String generatePageSql(String sql, Integer pageNo, Integer pageSize) {
        if(StringUtils.isBlank(sql)){
            throw new BusinessException("sql语句为空");
        }
        if(pageNo == null){
            pageNo = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }
        String result = sql;

        result = " SELECT ROWNUM AS rowno,r.* FROM ( " + sql + " ) r where ROWNUM <= " + (pageSize * pageNo);

        result = " SELECT * FROM ( " + result + "  ) table_alias WHERE table_alias.rowno > " + (pageSize * (pageNo - 1));
        return result;
    }
}
